
Page({
  data:{
  },
  onLoad(query) {
    
    this.getAuth();
  },

  //input change
  inputChange(e){

    if(e.currentTarget.dataset.name == 'name'){
      this.setData({
        name:e.detail.value
      })
    }else if(e.currentTarget.dataset.name == 'date'){
      this.setData({
        startTime:new Date(e.detail.value[0]).getTime(),      //投放开始时间戳
        endTime:new Date(e.detail.value[1]).getTime()         //投放结束时间戳
      })
    }else{
      if(e.detail.value.indexOf(',') == -1){
        let i=[];       //商品id组必须为数组且为数组类型
        i.push(parseInt(e.detail.value))
        this.setData({
          targetEntityList:i
        })
      }else{
        let arr=e.detail.value.split(',');
        let parseIntArr=[];
        for(let i in arr){
          parseIntArr.push(parseInt(arr[i]))
        }
        this.setData({
          targetEntityList:parseIntArr
        })
      }
    }

    //console.log("开始时间：",this.data.startTime,"结束时间：",this.data.endTime,"投放名称：",this.data.name,"商品组id：",this.data.targetEntityList)
  },

  //新建投放
  startZDTF(){

    getApp().name = this.data.name
    getApp().startTime = this.data.startTime
    getApp().endTime = this.data.endTime
    getApp().targetEntityList = this.data.targetEntityList


    if(this.data.name && this.data.startTime && this.data.endTime && this.data.targetEntityList){
      my.navigateTo({ url: '../toufang/toufang' })
    }else{
      my.alert({content:'投放信息填写不完整！'})
    }

  },

  //查看投放记录
  toufangJL(){
    my.qn.navigateToWebPage({
      url: "https://market.m.taobao.com/app/taefed/shopping-delivery-wapp/index.html#/putin?appId=3000000048925193",
      success: res => {
      },
      fail: res => {
      }
    })
  },

  //装修教程
  zxJC(){
    my.qn.navigateToWebPage({
      url: "https://www.yuque.com/docs/share/f1b393ab-7d33-4af5-9910-0c13502b875a?#",
      success: res => {
      },
      fail: res => {
      }
    })
  },

  getAuth() {//获取用户授权   ！！！！！使用投放插件必须全权授权！！！！！！
    my.authorize({
      scopes: '*',
      success: (res) => {
        my.getAuthUserInfo({
          success: (userInfo) => {
            //调用后端接口
            let option ={
              userNick:userInfo.nickName,
              avatar:userInfo.avatar
            }
            // getApp().cloud.function.invoke('business',option,"login").then(res => {
            //   my.alert({
            //     content:JSON.stringify(res)
            //   })
            // })
          },
          fail: (er) => {
            my.alert({ title: '获取失败', content: JSON.stringify(er) });
          }
        });
      },
      fail: (e) => {
        my.alert({ title: '授权失败', content: JSON.stringify(e) });
      }
    });
  
  },

  tf(){
      
      let link = `https://market.m.taobao.com/app/taefed/shopping-delivery-wapp/index.html#/putin?mainKey=form&appId=3000000048925193`;

       my.qn.naviateToWebPage({
        url: link,
        success: res => {

        },
        fail: res => {

        }
      })
      
    },
  onReady() {
    // 页面加载完成
  },
  onShow() {
    // 页面显示
  },
  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
  },
  onReachBottom() {
    // 页面被拉到底部
  },
  onShareAppMessage() {
    // 返回自定义分享信息
    return {
      title: 'My App',
      desc: 'My App description',
      path: 'pages/index/index',
    };
  },
});
